package org.buptsse.listview;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.buptsse.entity.CLHotPost;
import org.buptsse.entity.Circle;
import org.buptsse.entity.HistoryItem;
import org.buptsse.entity.User;
import org.buptsse.ui.LogoActivity;
import org.buptsse.ui.R;
import org.buptsse.ui.TradeHistoryActivity;
//import org.buptsse.ui.TreeActivity;
import org.buptsse.utils.APPConstant;
import org.buptsse.utils.ActionUtils;
import org.buptsse.utils.ConnectionChecker;
import org.buptsse.utils.GloableInstance;
import org.buptsse.utils.ImageService;
import org.buptsse.utils.JsonUtils;

import com.google.gson.reflect.TypeToken;

import android.R.bool;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HistoryBuyInPage extends LinearLayout implements OnScrollListener {
	
	private final String URL_CIRCLELIST="fetchJoinCircle.do";//——确定调用函数
	ListView list;//——信件一个list
	int scrollState;
	int count = 40;
	int lastItem;
	int visibleItemCount;
	//Button footerButton;
	//LinearLayout footerProgressBarLayout;// 加载进度滚动条
	ListAdapter listAdapter = new ListAdapter();
	//View viewfoot;
	private User user=null;//——一个用户
	private int uid;
	private boolean isDataLoaded=false;
	private List<HistoryItem> historyBuyInList = new LinkedList<HistoryItem>();//——新建一个叫circle类型的数组
	private Context context;
	private ListView listView = null;//——一个界面显示
	//——这个是函数
	Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case TradeHistoryActivity.LoadFinish:
				//System.out.println("msg.obj is:"+msg.obj);
				historyBuyInList=(List<HistoryItem>) msg.obj;
				//System.out.println("historyBuyInList is:"+historyBuyInList);
				if (null != historyBuyInList) {
					listAdapter.count = historyBuyInList.size();
				} else {
					listAdapter.count = 0;
				}
				//Toast.makeText(context, "size is"+historyBuyInList.size(), 1000).show();
				listAdapter.notifyDataSetChanged();
				break;
			case TradeHistoryActivity.NetError:
				Toast.makeText(context, "网络连接不可用", 1000).show();
				break;
			case TradeHistoryActivity.LoadError:
				Toast.makeText(context, "加载失败，请重试", 1000).show();
				break;
			case TradeHistoryActivity.NoData:
				Toast.makeText(context, "没有相关数据", 1000).show();
				break;
			}
			super.handleMessage(msg);
		}
	};

	public HistoryBuyInPage(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public HistoryBuyInPage(Context context, User user) {
		super(context);
		this.user = user;
		this.context = context;
		//uid=user.getId();
		uid=124;
		init();
	}

	/**
	 * 
	 */
	private void init() {
		setBackgroundColor(Color.parseColor("#ecf6eb"));
		listAdapter.count=0;
		listView = new ListView(getContext());
		listView.setDivider(new ColorDrawable(Color.parseColor("#ecf6eb")));
		listView.setDividerHeight(1);
		listView.setVerticalFadingEdgeEnabled(false);
		listView.setAdapter(listAdapter);
		listView.setOnScrollListener(this);
		/*listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				CLHotPost onSelect = hotPostList.get(arg2);
				Intent it = new Intent(context,LogoActivity.class);
				it.putExtra("user", user);
				it.putExtra("onSelect", onSelect);
				context.startActivity(it);
			}
		});*/
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		addView(listView, params);
	}
	
	@Override
	public Handler getHandler()
	{
		return myHandler;
	}

	class ListAdapter extends BaseAdapter {

		int count = 10;

		@Override
		public int getCount() {
			return count;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			View view = inflater.inflate(R.layout.listview_item_history_buy, null);
			HistoryItem item=historyBuyInList.get(position);
			//LinearLayout aa = (LinearLayout)view.findViewById(R.id.main_linear_layout);
			//图籍图片
			ImageView bookImage=(ImageView) view.findViewById(R.id.book_photo);
			bookImage.setImageBitmap(ImageService.getImageByUrl(item.getBookimage()));
			//书籍名称
			TextView bookName = (TextView) view.findViewById(R.id.bookname);
			bookName.setText(item.getBookname());
			//卖家
			TextView seller=(TextView)view.findViewById(R.id.seller);
			seller.setText(item.getSeller());
			//开始时间
			TextView beginTime=(TextView)view.findViewById(R.id.begintime);
			beginTime.setText(item.getBegintime());
			//结束时间
			TextView endTime=(TextView)view.findViewById(R.id.endtime);
			endTime.setText(item.getEndtime());
			//价格
			TextView price=(TextView)view.findViewById(R.id.price);
			price.setText(item.getPrice());
			return view;
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		this.visibleItemCount = visibleItemCount;
		lastItem = firstVisibleItem + visibleItemCount - 1;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		this.scrollState = scrollState;
	}
}
